﻿using IntoStationSystem.Global;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using static IntoStationSystem.Global.Events;

namespace IntoStationSystem.Dialog
{
    /// <summary>
    /// StartupDialog.xaml 的交互逻辑
    /// </summary>
    public partial class StartupDialog : Window
    {
        public StartupDialog()
        {
            InitializeComponent();
            worker = new BackgroundWorker() { WorkerReportsProgress = true };
            worker.DoWork += DoWork;
            worker.RunWorkerCompleted += RunWorkerCompleted;
            worker.ProgressChanged += ProgressChanged;
            loadProgress.Value = 0;
            this.DataContext = this;
        }

        BackgroundWorker worker;

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            //Load();
            worker.ReportProgress(5);
            Thread.Sleep(500);
            worker.ReportProgress(25);
            Thread.Sleep(500);
            worker.ReportProgress(35);
            Thread.Sleep(500);
            worker.ReportProgress(55);
            Thread.Sleep(500);
            worker.ReportProgress(75);
            Thread.Sleep(500);
            worker.ReportProgress(100);
            Thread.Sleep(500);
        }
        private void ProgressChanged(object sender, ProgressChangedEventArgs e) 
        {
            //tip.Text = "111";
            loadProgress.Value = e.ProgressPercentage;
        }
        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
            EventRepository.EventAggregator.GetEvent<StartCompletedEvent>().Publish();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            worker.RunWorkerAsync();
        }
    }
}
